<?php 
	require_once("init.php"); 
?>

<?php
	$page = 0;
	if (isset($_GET["page"])) {
		$page = $_GET["page"];
	}
	$startRow = $page * $topicsPerPage;
	$endRow = ($page+1) * $topicsPerPage;
	
	// Condition
	$strTopicsCondition1 = "";
	$strTopicsCondition2 = "";
	$strTopicsCondition3 = "";
	$strTopicsCondition4 = "";
	
	if (isset($_POST["keyword"])) {
		$search_keyword = $_POST["keyword"];
		$search_status = $_POST["status"];
	} else if (isset($_GET["keyword"])) {
		$search_keyword = $_GET["keyword"];
		$search_status = $_GET["status"];
	}
	
	if (isset($_SESSION["userId"])) {
		$strTopicsCondition1 = $strTopicsCondition1."
			AND t.topicId NOT IN (
				SELECT topicId
				FROM votes
				WHERE userId = ".$_SESSION["userId"]."
			)
		";
		$strTopicsCondition2 = $strTopicsCondition2."
			AND t.topicId IN (
				SELECT topicId
				FROM votes
				WHERE userId = ".$_SESSION["userId"]."
			)
		";
	} else if (strlen(session_id()) > 0) {
		$strTopicsCondition1 = $strTopicsCondition1."
			AND t.topicId NOT IN (
				SELECT topicId
				FROM votes
				WHERE sessionId = '".session_id()."'
			)
		";
		$strTopicsCondition2 = $strTopicsCondition2."
			AND t.topicId IN (
				SELECT topicId
				FROM votes
				WHERE sessionId = '".session_id()."'
			)
		";
	}
	if ($search_status == "open") {
		$strTopicsCondition3 = $strTopicsCondition3." 
			AND now() < dateClosed
		";
	} else if ($search_status == "closed") {
		$strTopicsCondition3 = $strTopicsCondition3." 
			AND now() >= dateClosed
		";
	}
	if (isset($_SESSION["categories"])) {
		$strTopicsCondition4 = $strTopicsCondition4."
			AND t.categoryId IN (".$_SESSION["categories"].")
		";
	}
	
	// Topics Count
	$strTopicsCount = "
    SELECT count(topicId) AS totalCount
		FROM topics t
		WHERE statusId = ".$statusActive."
			AND points > 0
			".$strTopicsCondition3."
			AND (
				question LIKE '%".$search_keyword."%'
				OR answer1 LIKE '%".$search_keyword."%'
				OR answer2 LIKE '%".$search_keyword."%'
			)
	";
	$strTopicsCount = $strTopicsCount."
    ORDER BY dateClosed DESC
		LIMIT 0, ".$maxTopicsNum."
	";
  $qTopicsCount = mysql_query($strTopicsCount);

  // Topics
	$strTopics = "
    SELECT
			t.*,
			c.name AS categoryName,
			(dateAdded >= subTime(now(), '24:00:00')) AS isNew,
			0 AS isVoted
		FROM topics t
			INNER JOIN categories c ON t.categoryId = c.categoryId
		WHERE statusId = ".$statusActive."
			AND points > 0
			".$strTopicsCondition1."
			".$strTopicsCondition3."
			".$strTopicsCondition4."
			AND (
				question LIKE '%".$search_keyword."%'
				OR answer1 LIKE '%".$search_keyword."%'
				OR answer2 LIKE '%".$search_keyword."%'
			)
		
		UNION
		
		SELECT
			t.*,
			c.name AS categoryName,
			(dateAdded >= subTime(now(), '24:00:00')) AS isNew,
			1 AS isVoted
		FROM topics t
			INNER JOIN categories c ON t.categoryId = c.categoryId
		WHERE statusId = ".$statusActive."
			AND points > 0
			".$strTopicsCondition2."
			".$strTopicsCondition3."
			".$strTopicsCondition4."
			AND (
				question LIKE '%".$search_keyword."%'
				OR answer1 LIKE '%".$search_keyword."%'
				OR answer2 LIKE '%".$search_keyword."%'
			)
	";

	$strTopics = $strTopics."
    ORDER BY dateClosed DESC
    LIMIT ".$startRow.", ".$endRow.";
  ";
  $qTopics = mysql_query($strTopics);
	
	$showMore = true;
	$totalCount = mysql_fetch_array($qTopicsCount);
	$totalCount = $totalCount["totalCount"];
	if ($endRow >= $totalCount) $showMore = false;
	if ($totalCount == 0) {
?>
<li>No topics found</li>
<?php
	} else {
		while ($row = mysql_fetch_array($qTopics)) {
?>
<li <?php if ($row["isNew"] == 1) print "class=\"new\""; ?>>
	<a href="viewtopic.php?id=<?php print $row["topicId"]; ?>">
		<?php print $row["question"]; ?><br />
		<small><strong><?php print $row["answer1"]." or ".$row["answer2"]; ?>?</strong></small>
	</a>
</li>
<?php
		}
	}
?>

<?php
	if ($showMore) {
?>
<li class="more">
	<a href="listsearchtopic.php?page=<?php print ($page+1); ?>&keyword=<?php print $search_keyword; ?>&status=<?php print $search_status; ?>" target="_replace"><?php print $topicsPerPage; ?> more topics...</a>
</li>
<?php
	}
?>